#include <stdio.h>
#include <stdlib.h>
#define NUM 100

//计算结果的函数
int func1(int a, char b, int c)
{
    switch(b)
    {
    case '+':
        return a + c;
    case '-':
        return a - c;
    case '*':
        return a * c;
    case '/':
        if ( c != 0 )
            return a / c;


    }
    return 0;
}

int main()
{
    FILE *p = fopen("a1.txt", "r");
    //while(!feof(p))

    char *array = calloc(NUM, sizeof(char));
    char *tmp = array;//代表当前要写入字符的位置
    int index = 0;


    while(1)
    {
        char buf[100] = { 0 };
        fgets(buf, sizeof(buf), p);

        if(feof(p))
            break;
        //取值
        int a = 0;
        char b = 0;
        int c = 0;
        sscanf(buf, "%d%c%d=", &a, &b, &c);
        //printf("%d%c%d=%d\n", a, b, c, func1(a, b, c));
        sprintf(tmp, "%d%c%d=%d\n", a, b, c, func1(a, b, c));
        array = realloc(array, NUM * (index + 2));
        tmp = array + (NUM * (index + 1));
        //array永远指向堆内存的首地址，tmp每次往后移动100个字节
        index++;
    }
    fclose(p);


    p = fopen("a1.txt", "w");
    int i;
    tmp = array;//让tmp继续回到起始位置
    for(i = 0; i < index; i++)
    {
        fputs(tmp, p);
        tmp += NUM;
    }
    fclose(p);
    free(array);

    return 0;

}

